package ae.pcfc.etks.online.itworx.ehs.web.compensation;

import ae.pcfc.etks.online.itworx.ehs.web.delegate.FNPDelegate;
import ae.pcfc.etks.online.itworx.ehs.web.enums.FNPRequestStatus;
import ae.pcfc.etks.online.itworx.ehs.web.model.Rollback;
import ae.pcfc.etks.online.itworx.ehs.web.util.UtilCommon;

public class FNPCompensationManager implements CompensationManager {

	private FNPDelegate fnpDelegate;

	public void setFnpDelegate(FNPDelegate fnpDelegate) {
		this.fnpDelegate = fnpDelegate;
	}

	@Override
	public boolean rollback(Object rollbackKey) throws Exception {
		if (rollbackKey != null) {
			Rollback rollback = (Rollback) rollbackKey;
			String paymentAdviceNumber = rollback.getFnpPaymentAdviceNumber();
			String transactionNumber = rollback.getFnpTransactionNumber();
			String userName = rollback.getFnpUserName();
			String message = rollback.getFnpMessage();
			if (rollback != null) {
				if (!UtilCommon.isEmpty(transactionNumber)
						&& !UtilCommon.isEmpty(paymentAdviceNumber)
						&& !UtilCommon.isEmpty(userName)) {
					String status = fnpDelegate
							.queryPaymentAdviceStatus(paymentAdviceNumber);
					if (status != null) {
						if (!FNPRequestStatus.PAYMENT_CONFIRMED.getStatusName()
								.equals(status)
								&& !FNPRequestStatus.PAYMENT_CANCELLED
										.getStatusName().equals(status)) {
							fnpDelegate.cancelPaymentAdvice(transactionNumber,
									paymentAdviceNumber, userName, message);
						}
					}
					return true;
				}
			}
		}
		return false;
	}
}
